/*<![CDATA[*/
var app5 = new Vue({
    el: '#app-5',
    data: {
        subComments: [],
        msg:"lal"
    },
    watch:{
        subComments:function () {
            for (var i=0;i<this.subComments.length;i++){
                var date = new Date(this.subComments[i].gmtCreate)
                var Str=date.getFullYear() + '-' +
                    (date.getMonth() + 1) + '-' +
                    date.getDate() + ' ' +
                    date.getHours() + ':' +
                    date.getMinutes() + ':' +
                    date.getSeconds();
                this.subComments[i].gmtCreate=Str;
            }
        }
    },
    methods: {
        firstLevelComment: function () {
            var questionId = $(".comment-content").attr("name");
            var content = $(".comment-content").val();
            this.comment(questionId, 1, content);
        },
        twoLevelComment:function (e) {
            var commentId = $(e.target).attr("data");
            var content = $("#twoLevelComment-" + commentId).val();
            this.comment(commentId, 2, content);
        },
        comment:function (parentId, type, content) {
            var url = "http://localhost:8081/comment";
            if (!content) {
                alert("不能回复空内容！")
                return;
            }
            $.ajax({
                type: "post",
                url: url,
                data: JSON.stringify({
                    "parentId": parentId,
                    "content": content,
                    "type": type
                }),
                datatype: "json",
                contentType: "application/json",
                success: function (res) {
                    console.log(res)
                    if (res.code == 200) {
                        window.location.reload();
                    } else if (res.code == 2003) {
                        var isAccept = confirm(res.message);
                        if (isAccept) {
                            window.open("https://gitee.com/oauth/authorize?client_id=6a7cdf6cd9332af128c8ada9b900e41bb13761f6688c360992acd3a92d380f99&redirect_uri=http://localhost:8081/callback&response_type=code");
                            localStorage.setItem("closeable", true);
                        }

                    } else {
                        alert(res.message);
                    }

                }
            });
        },
        collapseComments:function (e) {
            var that=this;
            var id = $(e.target).attr("data");
            var url = "http://localhost:8081/comment/" + id;
            if (!$(".collapseComments").hasClass("active")) {
                $.ajax({
                    type: "get",
                    url: url,
                    success: function (res) {
                    that.subComments=res.data;
                    }
                });
            }
            $(e.target).toggleClass("active");
            $("#firstLevelComment-" + id).toggleClass("in");
        }
    }
})